/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmArticulos.java
 *
 * Created on 31/10/2011, 10:48:50 AM
 */
package api.form;

import api.dom.Cliente;
import api.dom.Utilitaria;
import api.gest.GestCliente;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Luis
 */
public class Frmclientes extends javax.swing.JInternalFrame {

    /** Creates new form FrmArticulos */
    public Frmclientes() {
        initComponents();
        ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).setNorthPane(null);
         lblnumero.setText(String.valueOf(GestCliente.getInstance().devolverUltimoNum()));
        jTable1.setModel(Utilitaria.listarClientes((DefaultTableModel) jTable1.getModel()));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        lblnumero = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txttelefono = new javax.swing.JTextField();
        txtnombre = new javax.swing.JTextField();
        txtrut = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jDateChooser1 = new com.toedter.calendar.JDateChooser();
        jLabel7 = new javax.swing.JLabel();
        txtdireccion = new javax.swing.JTextField();
        jLabel21 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        txtNombre1 = new javax.swing.JTextField();
        btnBuscar = new javax.swing.JButton();
        btnBuscar1 = new javax.swing.JButton();
        btnagregar1 = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(720, 650));
        setMinimumSize(new java.awt.Dimension(720, 650));
        setNormalBounds(new java.awt.Rectangle(0, 0, 698, 581));
        setPreferredSize(new java.awt.Dimension(720, 650));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBackground(new java.awt.Color(153, 180, 209));
        jPanel1.setMaximumSize(new java.awt.Dimension(720, 74));
        jPanel1.setMinimumSize(new java.awt.Dimension(720, 74));
        jPanel1.setPreferredSize(new java.awt.Dimension(720, 74));
        jPanel1.setRequestFocusEnabled(false);

        jLabel6.setBackground(new java.awt.Color(51, 255, 51));
        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 48));
        jLabel6.setText("Clientes");
        jLabel6.setToolTipText("");

        jLabel8.setBackground(new java.awt.Color(51, 255, 51));
        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel8.setText("#");
        jLabel8.setToolTipText("");

        lblnumero.setBackground(new java.awt.Color(51, 255, 51));
        lblnumero.setFont(new java.awt.Font("Tahoma", 0, 24));
        lblnumero.setText("1");
        lblnumero.setToolTipText("");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 441, Short.MAX_VALUE)
                .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(lblnumero, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(46, 46, 46)
                .addComponent(jLabel9))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblnumero, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE))
                .addContainerGap())
        );

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 720, 74));

        jLabel1.setText("Fecha ingreso:");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 110, -1, 20));

        jLabel2.setText("Nombre:");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 80, -1, -1));

        jLabel3.setText("Rut:");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 140, -1, -1));

        jLabel4.setText("Telefono:");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(430, 80, -1, -1));
        getContentPane().add(txttelefono, new org.netbeans.lib.awtextra.AbsoluteConstraints(510, 80, 200, -1));
        getContentPane().add(txtnombre, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 80, 200, -1));
        getContentPane().add(txtrut, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 140, 190, -1));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Nombre", "Fecha  ingreso", "Rut", "Telefono", "Direccion"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 170, 700, 400));

        jButton1.setText("Eliminar");
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(620, 580, 90, 40));

        jButton2.setText("Agregar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 580, 90, 40));

        jButton3.setText("Modificar");
        jButton3.setEnabled(false);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(510, 580, 90, 40));
        getContentPane().add(jDateChooser1, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 110, 190, -1));

        jLabel7.setText("Direccion:");
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(430, 110, -1, -1));
        getContentPane().add(txtdireccion, new org.netbeans.lib.awtextra.AbsoluteConstraints(510, 110, 200, -1));

        jLabel21.setFont(new java.awt.Font("Tahoma", 2, 12));
        jLabel21.setText("Campos Obligatorios (*)");
        getContentPane().add(jLabel21, new org.netbeans.lib.awtextra.AbsoluteConstraints(570, 140, 140, -1));

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        txtNombre1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtNombre1KeyReleased(evt);
            }
        });
        jPanel2.add(txtNombre1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 150, -1));

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 580, 160, 40));

        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });
        getContentPane().add(btnBuscar, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 580, 90, 40));

        btnBuscar1.setText("...");
        btnBuscar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscar1ActionPerformed(evt);
            }
        });
        getContentPane().add(btnBuscar1, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 580, 20, 40));

        btnagregar1.setText("...");
        btnagregar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnagregar1ActionPerformed(evt);
            }
        });
        getContentPane().add(btnagregar1, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 140, 40, 20));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        if ("".equals(lblnumero.getText())) {
            JOptionPane.showMessageDialog(rootPane, "Falto ingresar codigo");
        }
        else if ("".equals(txtnombre.getText())) {
            JOptionPane.showMessageDialog(rootPane, "Falto ingresar nombre");
        }
        else if ("".equals(txtrut.getText())) {
            txtrut.setText("0");
        }
        else if ("".equals(txtdireccion.getText())) {
            txtdireccion.setText("0");
        }
        else if ("".equals(txttelefono.getText())) {
            txttelefono.setText("0");
        } else {
            //convierto los datos
            int codigo = Integer.parseInt(lblnumero.getText());
            String nombre = txtnombre.getText();
            String fing = Utilitaria.convertirDateString(jDateChooser1.getDate());
            String rut = txtrut.getText();
            String tel = txttelefono.getText();
            String direccion = txtdireccion.getText();
            //creo el cliente
            Cliente objC = new Cliente(codigo, nombre, fing, rut, tel, direccion);
            //lo agrego
            GestCliente.getInstance().altaCliente(objC);
            //mensaje
            JOptionPane.showMessageDialog(rootPane, "Cliente0 agregado exitosamente");
            //listo los articulos y cambio el nuevo numero
            Utilitaria.listarClientes((DefaultTableModel) jTable1.getModel());
            lblnumero.setText(String.valueOf(GestCliente.getInstance().devolverUltimoNum()));
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void txtNombre1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNombre1KeyReleased
        // TODO add your handling code here:
        try {
            DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
            modelo.setRowCount(0);
            String nombre = txtNombre1.getText();
            int i = nombre.length();
            String nombre2;
            for (Cliente objC : GestCliente.getInstance().devolverCliente()) {
                nombre2 = objC.getCliNom();
                String cortado = nombre2.substring(0, i);
                if (cortado.equalsIgnoreCase(nombre)) {
                    Object[] objart = {objC.getCliCod(), objC.getCliNom(), objC.getCliFing(), objC.getCliRut(), objC.getCliTel(), objC.getCliDire()};
                    modelo.addRow(objart);
                }
            }
        } catch (Exception e) {
        }
}//GEN-LAST:event_txtNombre1KeyReleased

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_btnBuscarActionPerformed

    private void btnBuscar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscar1ActionPerformed
        // TODO add your handling code here:
        this.jTable1.setModel(Utilitaria.listarClientes((DefaultTableModel) jTable1.getModel()));
        this.txtNombre1.setText("");
}//GEN-LAST:event_btnBuscar1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        if ("".equals(lblnumero.getText())) {
            JOptionPane.showMessageDialog(rootPane, "Falto ingresar codigo");
        }
        if ("".equals(txtnombre.getText())) {
            JOptionPane.showMessageDialog(rootPane, "Falto ingresar nombre");
        }
        if ("".equals(txtrut.getText())) {
            txtrut.setText("0");
        }
        if ("".equals(txtdireccion.getText())) {
            txtdireccion.setText("0");
        }
        if ("".equals(txttelefono.getText())) {
            txttelefono.setText("0");
        } else {
            //convierto los datos
            int codigo = Integer.parseInt(lblnumero.getText());
            String nombre = txtnombre.getText();
            String fing = Utilitaria.convertirDateString(jDateChooser1.getDate());
            String rut = txtrut.getText();
            String tel = txttelefono.getText();
            String direccion = txtdireccion.getText();
            //creo un articulo temporal
            Cliente objC = new Cliente(codigo, nombre, fing, rut, tel, direccion);
            //lo modifico
            GestCliente.getInstance().modificarCliente(objC);
            //listo los clientes
            Utilitaria.listarClientes((DefaultTableModel) jTable1.getModel());
            lblnumero.setText(String.valueOf(GestCliente.getInstance().devolverUltimoNum()));
            txtnombre.setText("");
            txtdireccion.setText("");
            txtrut.setText("");
            txttelefono.setText("");
            jDateChooser1.setDate(null);
            jButton3.setEnabled(false);
            jButton2.setEnabled(true);
            JOptionPane.showMessageDialog(rootPane, "Cliente modificado");
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        lblnumero.setText(String.valueOf(jTable1.getValueAt(jTable1.getSelectedRow(), 0)));
        txtnombre.setText(String.valueOf(jTable1.getValueAt(jTable1.getSelectedRow(), 1)));
        txtrut.setText(String.valueOf(jTable1.getValueAt(jTable1.getSelectedRow(), 3)));
        txtdireccion.setText(String.valueOf(jTable1.getValueAt(jTable1.getSelectedRow(), 5)));
        txttelefono.setText(String.valueOf(jTable1.getValueAt(jTable1.getSelectedRow(), 4)));
        this.jDateChooser1.setDate(Utilitaria.convertirStringDate(String.valueOf(jTable1.getValueAt(jTable1.getSelectedRow(), 2))));
        jButton3.setEnabled(true);
        jButton2.setEnabled(false);
    }//GEN-LAST:event_jTable1MouseClicked

    private void btnagregar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnagregar1ActionPerformed
        // TODO add your handling code here:
        lblnumero.setText(String.valueOf(GestCliente.getInstance().devolverUltimoNum()));
        txtnombre.setText("");
        txtdireccion.setText("");
        txtrut.setText("");
        txttelefono.setText("");
        jDateChooser1.setDate(null);
        jButton2.setEnabled(true);
        jButton3.setEnabled(false);
}//GEN-LAST:event_btnagregar1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnBuscar1;
    private javax.swing.JButton btnagregar1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private com.toedter.calendar.JDateChooser jDateChooser1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel lblnumero;
    private javax.swing.JTextField txtNombre1;
    private javax.swing.JTextField txtdireccion;
    private javax.swing.JTextField txtnombre;
    private javax.swing.JTextField txtrut;
    private javax.swing.JTextField txttelefono;
    // End of variables declaration//GEN-END:variables
}
